package cz.proks.easyhelpdesk.gae.core.manager;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cz.proks.easyhelpdesk.api.HelpdeskApi;
import cz.proks.easyhelpdesk.api.HelpdeskMemberApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.api.model.HelpdeskMember;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;

@Component
public class HelpdeskManager extends AManager<Helpdesk> implements HelpdeskApi {
	private final Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	@Qualifier(Configuration.Core.DB.HELPDESK_API)
	private HelpdeskApi dbApi;
	
	@Autowired
    @Qualifier(Configuration.Core.DB.HELPDESK_MEMBER_API)
    private HelpdeskMemberApi helpdeskMemberApi;

	@Override
	protected HelpdeskApi getDbApi() {
		return dbApi;
	}
	
	protected HelpdeskMemberApi getHelpdeskMemberDbApi() {
	    return helpdeskMemberApi;
	}

    @Override
    public List<Helpdesk> listForUser(Long userId) throws ApiException {
        log.debug("listForUser():: Start with userId '{}'", userId);
        
        List<Helpdesk> entityList = getDbApi().listForUser(userId);        
        
        log.debug("listForUser():: Exit with {} entities", entityList != null ? entityList.size() : null);
        return entityList;
    }	
    
    @Override
    public Long create(Helpdesk entity) throws ApiException {
        log.debug("create():: Start with helpdesk '{}'", entity);
        
        Long helpdeskId = super.create(entity);        
        
        log.debug("create():: Exit with id '{}'", helpdeskId);
        return helpdeskId;
    }
    
    private HelpdeskMember getDefaultHelpdeskMemberForHelpdeskOwner(Helpdesk helpdesk) {
        HelpdeskMember helpdeskMember = new HelpdeskMember();
        
        helpdeskMember.setHelpdeskId(helpdesk.getId());
        helpdeskMember.setUserId(helpdesk.getOwnerId());
        helpdeskMember.getHelpdeskRoles().add(HelpdeskRole.OPERATOR);
        
        return helpdeskMember;
    }
}